package com.zhoug.zmui.dialog.list;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zhoug.common.adapters.BaseListViewAdapter;
import com.zhoug.zmui.R;
import com.zhoug.zmui.utils.ZMUIUtils;
import com.zhoug.zmui.ints.OnSelectedListener;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.core.widget.ImageViewCompat;

/**
 * 单选列表对话框
 * zmui_style_ZMUISelectListDialog
 * 定义全局样式
 *
 * @Author 35574
 * @Date 2020/7/21
 * @Description
 */
public abstract class ZMUISelectListDialog<T> extends ZMUIAbsListDialog<T> {
    private static final String TAG = ">>>ZMUISelectListDialog";
    private static final boolean DEBUG = false;
    /**
     * 选择的item position
     */
    private int selected = -1;
    /**
     * 选择监听
     */
    private OnSelectedListener onSelectedListener;
    private ZMUIListAdapter mAdapter;
    /**
     * 勾选图标
     */
    private @DrawableRes
    int iconDrawableRes;
    /***
     * tint color
     */
    private ColorStateList tint;

    public ZMUISelectListDialog(@NonNull Context context) {
        super(context, 0, R.attr.zmui_style_ZMUISelectListDialog, 0);
    }

    @Override
    protected void initialize(@NonNull Context context, int defStyleAttr, int defStyleRes) {
        super.initialize(context, defStyleAttr, defStyleRes);
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZMUISelectListDialog, defStyleAttr, defStyleRes);
        iconDrawableRes = a.getResourceId(R.styleable.ZMUISelectListDialog_zmui_iconDrawable, 0);
        tint = a.getColorStateList(R.styleable.ZMUISelectListDialog_zmui_tint);
        a.recycle();
        setTextGravity(Gravity.START);
    }

    @Override
    protected void initListView(ListView listView) {
        mAdapter = new ZMUIListAdapter();
        mAdapter.setData(getData());
        listView.setAdapter(mAdapter);
        scrollToSelected(selected);
    }

    @Override
    public void show() {
        super.show();
        scrollToSelected(selected);
    }

    /**
     * 数据适配器
     */
    private class ZMUIListAdapter extends BaseListViewAdapter<T> implements View.OnClickListener {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.zmui_select_list_item, parent, false);
                TextView textView = convertView.findViewById(R.id.textView);
                ImageView imageView = convertView.findViewById(R.id.imageView);
                ZMUIUtils.setTextSize(textView, getTextSize());
                ZMUIUtils.setTextColor(textView, getTextColor());
                textView.setGravity(getTextGravity());
                if (iconDrawableRes != 0) {
                    imageView.setImageResource(iconDrawableRes);
                }
                if (tint != null) {
                    ImageViewCompat.setImageTintList(imageView, tint);
                }
            }
            TextView textView = convertView.findViewById(R.id.textView);
            ImageView imageView = convertView.findViewById(R.id.imageView);
            T itemData = getItemData(position);
            if (itemData != null) {
                textView.setText(getText(itemData));
                if (selected == position) {
                    imageView.setVisibility(View.VISIBLE);
                } else {
                    imageView.setVisibility(View.GONE);
                }
                convertView.setTag(position);
                convertView.setOnClickListener(this);
            } else {
                textView.setText(null);
                imageView.setVisibility(View.GONE);
                convertView.setOnClickListener(null);
            }
            return convertView;
        }

        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            if (selected != position) {
                selected = position;
                notifyDataSetChanged();
                if (onSelectedListener != null) {
                    onSelectedListener.onSelected(selected);
                }
            }
            dismiss();
        }
    }


    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    public int getSelected() {
        return selected;
    }

    public void setSelected(int selected) {
        this.selected = selected;
    }


    public void setIconDrawableRes(int iconDrawableRes) {
        this.iconDrawableRes = iconDrawableRes;
    }

    public void setTint(ColorStateList tint) {
        this.tint = tint;
    }

}
